<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="compony" :is-root="true" @addDept="changeShow" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{ data }" :tree-node="data" @editDept="editDept" @addDept="changeShow" @updateDepartments="getDepartments" />
        </el-tree>
      </el-card>
      <AddDept ref="editDept" :show-dialog="showDialog" :node="node" @success="success" @cancel="cancel" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { transListToTree } from '@/utils'

export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      compony: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      node: {},
      loading: false
    }
  },

  created() {
    this.getDepartments()
  },

  methods: {
    async getDepartments() {
      this.loading = true
      const res = await getDepartments()
      this.loading = false
      const { companyName, depts } = res
      this.compony = { name: companyName, manager: '负责人', id: '' }
      this.departs = transListToTree(depts, '')
    },
    changeShow(node) {
      this.showDialog = !this.showDialog
      // console.log(1)
      this.node = node
    },
    success() {
      // this.changeShow()
      this.changeShow()
      this.getDepartments()
    },
    cancel() {
      this.showDialog = false
    },
    editDept(node) {
      this.showDialog = true
      this.node = node
      this.$refs.editDept.getDeptInfo()
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
